package com.frxuexi.mdm2201.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.apps.FilePage
import com.frxuexi.apps.NetAppPage
import com.frxuexi.mdm2201.R

data class HomeEntity(val img: Int, val name: String, val type: Int)



@Composable
fun HomePage(navController: NavController= rememberNavController()) {
   /* var mylist = mutableListOf<HomeEntity>()
    val list = listOf<HomeEntity>(
        HomeEntity(R.drawable.icon1382, "应用", 1),

        HomeEntity(R.drawable.icon1488, "通信", 1),

        HomeEntity(R.drawable.icon352, "公众号", 1),
        HomeEntity(R.drawable.icon352, "知识中心", 1)
    )*/

    val list1= listOf<HomeEntity>(
        HomeEntity(R.drawable.icon32, "在线应用", 2),
        HomeEntity(R.drawable.icon32, "本地应用", 2),
        HomeEntity(R.drawable.icon1912, "考勤打卡", 2),
        HomeEntity(R.drawable.icon81, "智能审批", 2),
        HomeEntity(R.drawable.icon359, "工作日志", 2),
        HomeEntity(R.drawable.icon105, "任务", 2),
        HomeEntity(R.drawable.icon3, "文件", 2),
    )
    val list2= listOf<HomeEntity>(
        HomeEntity(R.drawable.icon1887, "联系人", 2),
        HomeEntity(R.drawable.icon980, "讨论组", 2),
        HomeEntity(R.drawable.icon1907, "组织架构", 2),
    )


    Column() {
        Row(modifier = Modifier
            .fillMaxWidth()
            .background(color = colorResource(id = R.color.gray))
            .padding(10.dp), verticalAlignment = Alignment.CenterVertically) {
            Text(
                text = "12",
                textAlign = TextAlign.Center,
                fontSize = 14.sp,
                color = Color.White,
                modifier = Modifier
                    .width(28.dp)
                    .background(color = colorResource(id = R.color.pink), shape = CircleShape)
                    .padding(5.dp)
            )
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "主门户")
        }
        Spacer(modifier = Modifier.height(10.dp))
        HomeItem1(homeEntity = HomeEntity(R.drawable.icon1382, "应用", 1))
        LazyVerticalGrid(verticalArrangement= Arrangement.spacedBy(15.dp),columns = GridCells.Adaptive(120.dp), content = {
            items(list1){entity->
                HomeItem2(homeEntity = entity,navController)
            }
        })
        Spacer(modifier = Modifier.height(10.dp))
        HomeItem1(homeEntity =  HomeEntity(R.drawable.icon1488, "通信", 1))
        Spacer(modifier = Modifier.height(10.dp))
        LazyVerticalGrid(verticalArrangement= Arrangement.spacedBy(15.dp),columns = GridCells.Adaptive(120.dp), content = {
            items(list2){entity->
                HomeItem2(homeEntity = entity,navController)
            }
        })
        Spacer(modifier = Modifier.height(20.dp))
        HomeItem1(homeEntity =  HomeEntity(R.drawable.icon352, "公众号", 1))
        Spacer(modifier = Modifier.height(20.dp))
        HomeItem1(homeEntity =  HomeEntity(R.drawable.icon352, "知识中心", 1))
    }

}

@Composable
fun HomeItem1(homeEntity: HomeEntity){
    Row {
        Spacer(modifier = Modifier.width(10.dp))
        Image(modifier = Modifier.width(30.dp), painter = painterResource(id = homeEntity.img), contentDescription = "")
        Spacer(modifier = Modifier.width(10.dp))
        Text(text = homeEntity.name)
    }
}

@Composable
fun HomeItem2(homeEntity: HomeEntity, navController: NavController){
    /*val navController = rememberNavController()
    NavHost(navController = navController, startDestination = ""){
        composable("online"){ NetAppPage()}
        composable("file"){ FilePage() }
    }*/
    Column(horizontalAlignment=Alignment.CenterHorizontally, modifier = Modifier.clickable {
//        onClick(homeEntity)
        if(homeEntity.name=="在线应用"){
//            navController.navigate("online")
            ARouter.getInstance().build("/apps/NetAppActivity").navigation()

        }else if(homeEntity.name=="文件"){
//            navController.navigate("file")
            ARouter.getInstance().build("/doc/DocActivity").navigation()
        }else if(homeEntity.name=="工作日志"){
            ARouter.getInstance().build("/report/ReportActivity").navigation()
        }
    }) {
        Image(modifier = Modifier.size(50.dp),contentScale= ContentScale.Crop, painter = painterResource(id = homeEntity.img), contentDescription = "")
        Spacer(modifier = Modifier.width(10.dp))
        Text(text = homeEntity.name)
    }
}




